#! /bin/bash
[ "$#" -eq 0 ] && echo "Please specify installation path!" && exit 1

echo "Installing vms..."

link_dir="$HOME"/bin
package_dir=$(cd -P $(dirname "${0}"); pwd)
# check $1 is exists/empty or not
[ ! -d "${1}" ] && mkdir -p "${1}"
install_dir=$(cd -P "${1}"; pwd)
[[ $(ls -A ${install_dir} | wc -w) != "0" ]] && echo "Target directory is NOT EMPTY!" && exit 1
mkdir "${install_dir}"/log

[ ! -d "${link_dir}" ] && mkdir -p "${link_dir}"
mv "${package_dir}"/* "${install_dir}"
mv "${install_dir}"/uninstall "${package_dir}"
ln -s "${install_dir}"/vms "${link_dir}"/vms
rm -f "${install_dir}"/install

echo "${install_dir}" > "${package_dir}"/install.path

echo "Install complete."